package com.deer.wms.base.system.web.zhBusiness.in;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.deer.wms.base.system.finalUtil.BoxInfoState;
import com.deer.wms.base.system.finalUtil.CellInfoState;
import com.deer.wms.base.system.finalUtil.TaskInfoState;
import com.deer.wms.base.system.model.box.BoxInfo;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.model.item.ItemInfo;
import com.deer.wms.base.system.model.task.TaskInfoDto;
import com.deer.wms.base.system.model.ware.CellInfo;
import com.deer.wms.base.system.model.zh.*;
import com.deer.wms.base.system.service.BillInRecordService;
import com.deer.wms.base.system.service.bill.IBillInDetailService;
import com.deer.wms.base.system.service.bill.IBillInMasterService;
import com.deer.wms.base.system.service.box.BoxInfoService;
import com.deer.wms.base.system.service.box.IBoxItemService;
import com.deer.wms.base.system.service.item.IItemInfoService;
import com.deer.wms.base.system.service.task.BillInTaskInfoService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.base.system.service.task.PickTaskService;
import com.deer.wms.base.system.service.task.TaskInfoHistoryService;
import com.deer.wms.base.system.service.ware.ICellInfoService;
import com.deer.wms.base.system.service.zh.*;
import com.deer.wms.base.system.util.JedisServiceUtil;
import com.deer.wms.base.system.util.RedisUtil;
import com.deer.wms.base.system.web.zhBusiness.business.QualityController;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 李帅辰
 * @date 2021/7/20 14:12
 */
@RestController
@RequestMapping("/testBillIn")
@Slf4j
public class testBillInController {
    @Autowired
    private ITaskInfoService taskInfoService;//任务处理层

    @Autowired
    private TaskInfoHistoryService taskInfoHistoryService;

    @Autowired
    private IBillInDetailService billInDetailService;//入库单明细

    @Autowired
    private IBillInMasterService billInMasterService;


    @Autowired
    private IBoxItemService boxItemService;//组盘

    @Autowired
    private IngotIocusService ingotIocusService;


    @Autowired
    private IItemInfoService itemInfoService;

    @Autowired
    private BoxInfoService boxInfoService;

    @Autowired
    private ICellInfoService cellInfoService;

    @Autowired
    private JedisServiceUtil jedisServiceUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ShelfPlanService shelfPlanService;

    @Autowired
    private BillInTaskInfoService billInTaskInfoService;

    @Autowired
    private QualityService qualityService;

    @Autowired
    private PickTaskService pickTaskService;

    @Autowired
    private QualityController qualityController;

    @Autowired
    private BoxItemExceptionService boxItemExceptionService;

    @Autowired
    private BillInRecordService billInRecordService;

    @Autowired
    private ZhMesService zhMesService;


    @PostMapping("/test")
    @ResponseBody
    @Transactional
    public Result test(String materialId) throws Exception {
        TaskInfoDto taskInfoDto = new TaskInfoDto();//任务对象
        CellInfo cellInfo = cellInfoService.findPaiAndfloorByShelfId(430);
        taskInfoDto.setShelfId(430);
        //taskInfoDto.setsFloor(cellInfo.getsFloor());
        //taskInfoDto.setsPai(cellInfo.getsPai());
        taskInfoDto.setType(TaskInfoState.BILLIN_TYPE);//任务类型
        long taskNumber = taskInfoService.generateTaskNumber();//设置任务号
        taskInfoDto.setTaskNumber(taskNumber);
        taskInfoDto.setState(TaskInfoState.STATE_ZERO);//状态
        taskInfoDto.setBillInDetailId(2286);//明细ID
        taskInfoDto.setBillId(1285);//入库单ID
        //taskInfoDto.setQuantity(1);
        //taskInfoDto.setMaterialId(materialId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taskInfoDto.setTaskStartTime(simpleDateFormat.format(new Date()));
        //taskInfoDto.setBoxCode(materialId);
        int i1 = billInTaskInfoService.insertTaskInfo(taskInfoDto);

        TaskInfoDto taskInfoDto1 = billInTaskInfoService.getAllUpdateByTaskNumber(taskNumber);//通过任务号查询更新报表所需要的数据
        //更新库存
        CellInfo cellBySheId = cellInfoService.getCellBySheId(taskInfoDto.getShelfId());//缓存线入库更新货位时查询货位
        Integer cellId = cellBySheId.getCellId();
        BoxItem boxItem = new BoxItem();
        BoxInfo boxInfo = new BoxInfo();
        //boxItem.setBoxCode("TP"+taskInfoDto1.getMaterialId());
        boxItem.setBatch(taskInfoDto1.getBatch());
        //boxItem.setItemCode(taskInfoDto1.getItemCode());
        boxItem.setBillInDetailId(taskInfoDto1.getBillInDetailId());
        //boxItem.setPd(taskInfoDto1.getPdTime());
        boxItem.setWareId(1220);
        boxItem.setState(0);
        //boxItem.setQuantity(1);
        BoxItem verifyBoxItem= boxItemService.verifyBoxItem(boxItem);
        if(null ==verifyBoxItem){
            int i = boxItemService.insertBoxItem(boxItem);
        }

        //boxInfo.setBoxCode("TP"+taskInfoDto.getMaterialId());
        boxInfo.setBoxState(BoxInfoState.BOXINFO_STATE_ONT);//0-不在货位 1-货位上  2-任务中
        boxInfo.setHasGoods(BoxInfoState.BOXINFO_GOODS_ONT);//   0-无货 1-有货
        boxInfo.setBoxCellId(cellId);//货位
        boxInfo.setBoxType(2);//类型：1-料箱  2-托盘
        boxInfoService.save(boxInfo);


        //货位以及状态 cellInfo
        ItemInfo byItemCode = itemInfoService.findByItemCode(taskInfoDto1.getItemCode());
        CellInfo cellInfo1 = new CellInfo();
        cellInfo1.setState(CellInfoState.STATE_ONT);//状态  ：0-无托盘；1-有货；2-出库中；3-入库中；4故障
        cellInfo1.setCellId(cellId);
        int editCellInfo = cellInfoService.updateCellInfo(cellInfo1);

        //对接mes 1:晶棒入缓存线，发送位置信息给MES
        ShelfPlan shelfPlan = new ShelfPlan();
        shelfPlan.setShelfId(taskInfoDto1.getShelfId());
        String shelfCode = shelfPlanService.findShelfPlanByshelfPlan(shelfPlan).getShelfCode();
        CacheLine cacheLine =new CacheLine();
        //cacheLine.setMaterialId(taskInfoDto1.getMaterialId());
        cacheLine.setStockId(shelfCode);
        cacheLine.setUserId("WMS");
        cacheLine.setStockInTime(DateUtil.now());//入库时间，12位，YYMMDDHHMMSS，MES根据入库时间排序
        String s = zhMesService.CacheLineLocationMes(cacheLine);
        log.info("晶棒入缓存线，发送位置信息给MES返回值:"+s);


        //WMS同步单晶信息给MES
        List<ZhItem> zhItems = new ArrayList<>();
        ZhItem zhItem  = new ZhItem();
        //zhItem.setMaterialId(taskInfoDto1.getMaterialId());
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyMMddHHmmss");
        Date date2 = new Date();
        zhItem.setStockInTime(dateFormat.format(date2));//入库时间
        zhItems.add(zhItem);


        //MES 晶棒缓存线同步库存信息给MES
        BillInUpdateBox billInUpdateBox = new BillInUpdateBox();
        billInUpdateBox.setStockId(shelfCode);
        billInUpdateBox.setInfo(zhItems);
        String s1 = zhMesService.BillInUpdateBoxMes(billInUpdateBox);
        log.info("晶棒缓存线同步库存信息给MES返回值:"+s1);
        return  ResultGenerator.genSuccessResult();

    }


    /**
     *MES请求的路径
     * @author 李帅辰
     * @date 2021/7/17 15:23
     * @return com.deer.wms.base.system.model.zh.ZhResult
     */
    @PostMapping("/mesBillOut")
    @ResponseBody
    @Transactional
    public ZhResult mesBillOut(@RequestBody ZhParam zhParam) {
        String op = zhParam.getOp();
        log.info("MES清求的接口:"+op);
        if (op.equals("NMSendStockOutDJK")){
            log.info("mes派工"+zhParam.toString());
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date2 = new Date();

            String jsonString = zhParam.getJsonString();
            ZhBillOutModel zhBillOutModel = JSONArray.parseObject(jsonString, ZhBillOutModel.class);
            ZhResult zhResult =new ZhResult();
            zhResult.setCode(0);
            zhResult.setId(zhBillOutModel.getMaterialId());
            zhResult.setMsg("Successfully");
            zhResult.setDate(dateFormat.format(date2));
            return  zhResult;
        }
        if (op.equals("NMDJInZBJ")) {
            String jsonString = zhParam.getJsonString();
            GlueStickRoom glueStickRoom = JSONArray.parseObject(jsonString, GlueStickRoom.class);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date2 = new Date();
            ZhResult zhResult = new ZhResult();
            zhResult.setCode(0);
            zhResult.setId(glueStickRoom.getLotId());
            zhResult.setMsg("接收成功");
            zhResult.setDate(dateFormat.format(date2));
            return zhResult;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = new Date();
        ZhResult zhResult = new ZhResult();
        zhResult.setCode(1);
        zhResult.setMsg("失败,不存在此接口信息");
        zhResult.setDate(dateFormat.format(date2));
        return zhResult;

    }

    @PostMapping("/CancelAccount")
    @ResponseBody
    @Transactional
    public String CancelAccount(){
        CacheLine cacheLine = new CacheLine();
        cacheLine.setMaterialId("456789");
        cacheLine.setStockId("HC-B4");
        cacheLine.setUserId("WMS");
        String s = zhMesService.CancelAccount(cacheLine);
        return s;
    }
}
